package game;

public class GridBoard extends Board
{

	public GridBoard(int rows, int columns)
	{
		this.rows = rows;
		this.columns = columns;
		
		itsGameModels = new GameModel[rows][columns];
		for(int i = 0; i < rows; ++i)
		{
			for(int j = 0; j < columns; ++j)
			{
				itsGameModels[i][j] = null;
			}
		}
	}
	
	@Override
	public GameModel getGameModel(BoardPosition position)
	{
		if(position instanceof GridPosition)
		{
			GridPosition gridPos = (GridPosition)position;
			return itsGameModels[gridPos.getX()][gridPos.getY()];
		}
		else
		{
			return null;
		}
	}

	@Override
	public void setGameModel(GameModel model, BoardPosition position)
	{
		if(position instanceof GridPosition)
		{
			GridPosition gridPos = (GridPosition)position;
			itsGameModels[gridPos.getX()][gridPos.getY()] = model;
		}
	}
	
	@Override
	public boolean isFull()
	{
		boolean full = true;
		
		for(int i = 0; i < rows; ++i)
		{
			for(int j = 0; j < columns; ++j)
			{
				if(itsGameModels[i][j] == null)
				{
					full = false;
					break;
				}
			}
		}
		return full;
	}
	
	@Override
	public void display()
	{
		System.out.println("------");
		for(int i = 0; i < rows; ++i)
		{
			for(int j = 0; j < columns; ++j)
			{
				if(itsGameModels[i][j] != null)
				{
					System.out.print(itsGameModels[i][j].getDescriptor());
				}
				else
				{
					System.out.print(" ");
				}
				if(j < columns - 1)
				{
					System.out.print(",");
				}
			}
			System.out.print("\n");
		}
		System.out.println("------\n");
	}
	
	private int rows, columns;
	GameModel [][] itsGameModels;
}
